<template>
  <div>
    <header class="header">
      <div @click="goBack">
        <van-icon name="arrow-left" size="24"></van-icon>
      </div>
      <van-search v-model="searchInput" placeholder="请输入搜索关键词" />
      <span @click="goList">搜索</span>
    </header>
    <scroll class="content">
      <div class="hot">
        <div class="hot-hd">
          <div>
            <i class="iconfont icon-remen" style="color: #b0352f;margin-right: 5px;"></i>
            <span>历史搜索</span>
          </div>
          <span @click="clearAll">清空</span>
        </div>
        <div class="hot-bd">
          <a v-for="(item, index) in historyData" :key="index">{{ item }}</a>
        </div>
      </div>
      <div class="recommend">
        <i class="iconfont icon-thumbup"></i>
        <span>推荐商品</span>
      </div>
      <good-list :goodList="goodList"></good-list>
    </scroll>
  </div>
</template>

<script>
import Scroll from '@/components/common/Scroll.vue'
import GoodList from '@/components/content/home/GoodList.vue'
import { Dialog } from 'vant'
import { getLikeData } from '@/network/api/home'
export default {
  name: 'search',
  components: {
    Scroll,
    GoodList
  },
  data() {
    return {
      searchInput: '',
      searchArr: [],
      historyData: [],
      goodList: []
    }
  },
  methods: {
    goBack() {
      this.$router.push('/home')
    },
    goList() {
      if (this.searchInput.trim() === '' || this.searchInput === '') return
      // 判断本地存储里有没有记录
      const res = JSON.parse(localStorage.getItem('searchValue')) || []
      if (res.length !== 0) {
        this.searchArr = res
      }
      this.searchArr.unshift(this.searchInput)
      // 数组去重
      this.searchArr = Array.from(new Set(this.searchArr))
      localStorage.setItem('searchValue', JSON.stringify(this.searchArr))
      this.$router.push('/search-list')
      // console.log(localStorage.getItem('searchValue'));
    },
    getHistoryData() {
      this.historyData = JSON.parse(localStorage.getItem('searchValue'))
    },
    beforeHistoryClear(action, done) {
      if (action === 'confirm') {
        // 删除localStorage中的内容
        setTimeout(() => {
          localStorage.removeItem('searchValue')
          this.historyData = []
          done()
        }, 200);

      } else {
        done();
      }

    },
    clearAll() {
      Dialog.confirm({
        message: '是否删除全部历史搜索',
        beforeClose: this.beforeHistoryClear
      }).catch(err => err)
    }
  },
  async created() {
    // 获取历史搜索
    this.getHistoryData()
    const res = await getLikeData()
    this.goodList = res.data
  }
}
</script>

<style lang='less' scoped>
.header {
  display: flex;
  align-items: center;
  background-color: #b0352f;

  .van-icon {
    color: #fff;
    margin-left: 5px;
  }

  span {
    color: #fff;
    margin-right: 10px;
  }

  .van-search {
    flex: 1;
    background-color: #b0352f;
  }

  .van-search__content {
    border-radius: 15px;
  }
}

.content {
  overflow: hidden;
  height: calc(100vh - 104px);

  .hot {
    padding: 15px;

    .hot-hd {
      display: flex;
      justify-content: space-between;
      margin-bottom: 15px;
    }

    .hot-bd {
      a {
        display: inline-block;
        font-size: 14px;
        border: 1px solid #ddd;
        padding: 5px;
        color: #999;
        margin: 0 25px 20px 0;
      }
    }
  }

  .recommend {
    margin: 15px;

    .iconfont {
      font-size: 16px;
      color: #b0352f;
      margin-right: 5px;
    }
  }
}
</style>